﻿using Moq;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace IHttpClient.Implementation.Moq
{
    public class HttpClientMock
    {
        public static Mock<System.Net.Http.IHttpClient> GetHttpClientMock(string stringContent)
        {
            if (string.IsNullOrEmpty(stringContent))
                throw new ArgumentNullException(stringContent);

            var clientMock = new Mock<System.Net.Http.IHttpClient>();

            clientMock.SetupAllProperties();

            // You HttpRequestHeaders has an internal constructor, so we can't access that,
            // HttpClient however creates one internally. We can use that one for our tests.
            var requestHeaders = new HttpClient().DefaultRequestHeaders;

            clientMock
                .SetupGet(call => call.DefaultRequestHeaders)
                .Returns(requestHeaders);

            var httpContent = new StringContent(stringContent);

            clientMock
                .Setup(call => call.GetAsync(It.IsAny<Uri>()))
                .Returns<Uri>(_ => Task.FromResult<HttpResponseMessage>(new HttpResponseMessage(HttpStatusCode.Accepted)
                {
                    Content = httpContent
                }));
            
            return clientMock;
        }
    }
}